﻿using System;
using System.Diagnostics.Contracts;
using System.IO;

namespace OpenLS.Spreadsheet.Formats.Biff.OfficeArt
{
    internal struct MSOCR
    {
        public static MSOCR  FromRGBI (byte red, byte green, byte blue, bool isThemeIndex)
        {
            MSOCR result = new MSOCR();
            result.red = red;
            result.blue = blue;
            result.green = green;
            result.isThemeIndex = isThemeIndex;
            return result;
        }
        internal byte red;
        internal byte green;
        internal byte blue;
        internal bool isThemeIndex;
        private byte A;
        private byte unused;

        byte ABunused2{get
        {
            byte result = A;
            if (isThemeIndex)
                result |= 0x8;
            result |= (byte)(unused << 4);
            return result;
        }}

        public static int ByteLength
        {
            get { return 4; }
        }

        public void Read(BinaryReader reader)
        {
            this.red = reader.ReadByte();
            green = reader.ReadByte();
            this.blue = reader.ReadByte();
            var abUnused = reader.ReadByte();
            this.A = (byte) (abUnused & 0x7);
            this.isThemeIndex = ((abUnused >> 3) & 0x01) != 0;
            this.unused = (byte) (abUnused >> 4);
            Contract.Assert(this.ABunused2 == abUnused);
        }

        public void Write(BinaryWriter writer)
        {
            writer.Write(red);
            writer.Write(green);
            writer.Write(blue);
            writer.Write(ABunused2);
        }
    }
}